/**
 * 2017年8月22日
 */
package exp.core;

import java.io.File;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import utilities.ClassifierTools;
import weka.core.Instances;

/**
 * 该类用于读取arff文件,可以读取的文件名称如下列出<br>
 * Beef CBF Car Coffee Cricket ECG200 FISH FaceAll FaceFour Gun_Point Lighting2
 * Lighting7 OSULeaf OliveOil Passgraph Plane Sony SwedishLeaf Trace
 * Two_Patterns synthetic_control wafer yoga <br>
 * 使用的Linux命令列出(未必是最优的) ls -l | awk '{print $9}' | egrep '(.*)_TRAIN.arff' | sed
 * 's/_TRAIN.arff//g'
 * 
 * @author Alex
 */
@Component
public class InstancesLoader {
	public static interface InstancesHandler {
		public void accpet(String name, Instances inst);
	}

	public static interface PairInstancesHandler {
		public void accpet(String name, Instances train, Instances test);
	}

	public final static String names[] = new String[] { 
			"Beef",
			"CBF",
			"Coffee",
			"ECG200",
			"FISH",
			"FaceAll",
			"FaceFour",
			"Gun_Point",
			"Lighting2",
			"Lighting7",
			"OSULeaf",
			"OliveOil",
			"SwedishLeaf",
			"Synthetic_control",
			"Trace",
			"Two_Patterns",
			"Wafer",
			"Yoga"
 };
	@Value("#{setting[dataset_dir]}")
	String baseDir = "D:\\wekadataset\\timeseries\\exp";

	public Instances load(String filename) {
		return ClassifierTools.loadData(new File(baseDir, filename).getAbsolutePath());
	}

	public Instances loadTrain(String filename) {
		String tfile = filename + "_TRAIN";
		return load(tfile);
	}

	public Instances loadTest(String filename) {
		String testFile = filename + "_TEST";
		return load(testFile);
	}

	public void forPairInstances(PairInstancesHandler ih) {
		for (int i = 0; i < names.length; i++) {
			ih.accpet(names[i], loadTrain(names[i]), loadTest(names[i]));
		}
	}

	public void forEachTrainInstances(InstancesHandler ih) {
		for (int i = 0; i < names.length; i++) {
			ih.accpet(names[i], loadTrain(names[i]));
		}
	}

	static String[] small = new String[] { "Sony", "Cricket", "CBF", "Coffee" };

	public void forEachSmallInstances(InstancesHandler ih) {

		for (int i = 0; i < small.length; i++) {
			ih.accpet(names[i], loadTrain(small[i]));
		}
	}

}
